<?php

/**
 * Class MediationorganController
 */
class MediationorganController extends Controller_Base
{
    public function init()
    {
        parent::init();
        SmartyAdapter::assign('type', Module_InfoBase_MediationOrgan::$type);
    }

    /**
     * 用户列表
     */
    public function indexAction()
    {
        $request = $this->getRequest();
        $pn = intval($request->getQuery('page', 1));
        $pageQuery = [];
        $whereQuery =$this->getWhere('mediatorId',[],'id');;
        $pn = $pn ? $pn : 1;
        $pageSize = 10;
        $start = ($pn - 1) * $pageSize;
        $total = Module_InfoBase_MediationOrgan::getInstance()->getAllBySearch(['count(id) as total'], ['where' => $whereQuery]);
        $total = $total[0]['total'];
        //计算分页
        $totalPage = ceil($total / $pageSize);
        if ($pn > $totalPage && $totalPage > 0) {
            $this->redirect('/mediationorgan/index?page=' . $totalPage);
            exit;
        }
        $list = Module_InfoBase_MediationOrgan::getInstance()->getAllBySearch(['*'], array('where' => $whereQuery, 'start' => $start, 'order' => 'id DESC', 'limit' => 10));
        SmartyAdapter::assign('list', $list);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $pageSize);
        SmartyAdapter::assign('curPageNum', $pn);

        SmartyAdapter::assign('page_query', http_build_query($pageQuery));
        SmartyAdapter::$templateName = 'mediationorgan/index';
    }

    /**
     * 删除用户
     */
    public function delAction()
    {
        $request = $this->getRequest();
        $id = intval($request->getPost('id'));
        if (!$id) {
            AjaxTool::returnJsonData(4001, Setup_Error::getConfig(4001), []);
        }
        $firm = Module_InfoBase_MediationOrgan::getInstance()->getItemById($id);
        if (!$firm) {
            AjaxTool::returnJsonData(4005,'找不到平台信息', []);
        }
        $res = Module_InfoBase_MediationOrgan::getInstance()->deleteItemById($id);
        if (!$res) {
            AjaxTool::returnJsonData(4000, '删除失败', []);
        }
        AjaxTool::returnJsonData(200, Setup_Error::getConfig(200), []);
    }

    /**
     * 原告库添加
     */
    public function addAction() {
        SmartyAdapter::$templateName = 'mediationorgan/addandedit';

    }

    /**
     *修改或编辑
     */
    public function addAndEditAction() {
        $id = $this->getRequest()->getPost('id', 0);
        $post_data = $this->getRequest()->getPost('mediationorgan', []);
        if (empty($post_data['name'])) {
            AjaxTool::returnJsonData(4001,'请填写调解部门名称', []);
        }
        if (empty($post_data['type'])) {
            AjaxTool::returnJsonData(4001,'请选择类型', []);
        }
        if(!empty($post_data['whiteIp'])){
            $whiteIp = explode('|',$post_data['whiteIp']);
            foreach ($whiteIp as $key => $ip){
                if(!filter_var(str_replace('.*','.0',$ip), FILTER_VALIDATE_IP)){
                    AjaxTool::returnJsonData(400,"ip : {$ip}，格式有误",[]);
                }

            }
        }
        $ts = time();
        if ($id) {
            $post_data['updateTime'] = $ts;
            $res = Module_InfoBase_MediationOrgan::getInstance()->update($post_data, $id);
        } else {
            $post_data['addTime'] = $ts;
            $post_data['updateTime'] = $ts;
            $res = Module_InfoBase_MediationOrgan::getInstance()->insert($post_data);
        }
        $msg = $id ? '编辑' : '添加';

        if (!$res) {
            AjaxTool::returnJsonData(400,$msg.'失败',[]);
        }
        AjaxTool::returnJsonData(200,$msg.'成功',[]);
    }

    /**
     * 编辑
     */
    public function editAction() {
        $id = $this->getRequest()->getQuery('id', 0);
        if (!$id) {
            $this->displayError('非法参数', '/mediationorgan/index');
        }
        $data = Module_InfoBase_MediationOrgan::getInstance()->getItemById($id);

        if (!$data) {
            $this->displayError('不存在该平台信息', '/mediationorgan/index');
        }
        SmartyAdapter::assign('info', $data);
        SmartyAdapter::$templateName = 'mediationorgan/addandedit';

    }
    public function ipRestrictAction(){
        $recordId = $this->getRequest()->getPost('id', 0);
        $ipRestrict = $this->getRequest()->getPost('ipRestrict', 0);
        $roleInfo = Module_InfoBase_MediationOrgan::getInstance()->getItemById($recordId);
        if(empty($roleInfo)){
            AjaxTool::returnJsonData(400, '找不到相应记录');
        }
        $res = Module_InfoBase_MediationOrgan::getInstance()->updateIpRestrictById($recordId, $ipRestrict);
        $type = $ipRestrict == 1 ?'开启':"关闭";
        if($res){
            AjaxTool::returnJsonData(200, "{$type}成功");

        }
        AjaxTool::returnJsonData(400, "{$type}失败");


    }
}